@use 'sass:math';
@use 'sass:color';

@use '_variables' as *;

@import '_bootstrap-mixins';

@mixin disable-default-a-behaviour {
  &:hover,
  &:focus,
  &:active {
    text-decoration: none !important;
  }

  &:focus:not(.focus-visible) {
    outline: none !important;
  }
}

@mixin disable-outline {
  &:focus:not(.focus-visible) {
    outline: none !important;
  }
}

@mixin ellipsis {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

@mixin muted {
  color: pvar(--greyForegroundColor) !important;
}

@mixin fade-text ($fade-after, $background-color) {
  position: relative;
  overflow: hidden;

  &::after {
    content: '';
    pointer-events: none;
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
    background: linear-gradient(transparent $fade-after, $background-color);
  }
}

@mixin peertube-word-wrap ($with-hyphen: true) {
  word-break: break-word;
  word-wrap: break-word;
  overflow-wrap: break-word;

  @if $with-hyphen {
    hyphens: auto;
  }
}

@mixin apply-svg-color ($color) {
  ::ng-deep .feather,
  ::ng-deep .material,
  ::ng-deep .misc {
    color: $color;
  }
}

@mixin fill-svg-color ($color) {
  ::ng-deep svg {
    path {
      fill: $color;
    }
  }
}

@mixin button-focus($color) {
  &:focus,
  &.focus-visible {
    box-shadow: #{$focus-box-shadow-form} $color;
  }
}

@mixin rounded-line-height-1-5 ($font-size) {
  line-height: $font-size + math.round(math.div($font-size, 2));
}

@mixin peertube-input-text($width, $font-size: $form-input-font-size) {
  @include rounded-line-height-1-5($font-size);

  font-size: $font-size;

  padding: 3px 15px;
  display: inline-block;
  width: $width;
  max-width: $width;
  color: pvar(--inputForegroundColor);
  background-color: pvar(--inputBackgroundColor);
  border: 1px solid pvar(--inputBorderColor);
  border-radius: 3px;

  &::placeholder {
    color: pvar(--inputPlaceholderColor);
  }

  &[readonly] {
    opacity: 0.7;
  }

  @media screen and (max-width: calc(#{$width} + 40px)) {
    width: 100%;
  }
}

@mixin peertube-textarea ($width, $height) {
  @include peertube-input-text($width);

  color: pvar(--textareaForegroundColor) !important;
  background-color: pvar(--textareaBackgroundColor) !important;
  height: $height;
  padding: 5px 15px;
}

@mixin orange-button {
  @include button-focus(pvar(--mainColorLightest));

  &,
  &:active,
  &:focus {
    color: #fff;
    background-color: pvar(--mainColor);
  }

  &:hover {
    color: #fff;
    background-color: pvar(--mainHoverColor);
  }

  &[disabled],
  &.disabled {
    cursor: default;
    color: #fff;
    background-color: pvar(--inputBorderColor);
  }

  my-global-icon {
    @include apply-svg-color(#fff);
  }
}

@mixin orange-button-inverted {
  @include button-focus(pvar(--mainColorLightest));

  padding: 2px 13px;
  border: 2px solid pvar(--mainColor);
  font-weight: $font-semibold;

  &,
  &:active,
  &:focus {
    color: pvar(--mainColor);
    background-color: pvar(--mainBackgroundColor);
  }

  &:hover {
    color: pvar(--mainColor);
    background-color: pvar(--mainColorLightest);
  }

  &[disabled],
  &.disabled {
    cursor: default;
    color: pvar(--mainColor);
    background-color: pvar(--inputBorderColor);
  }

  my-global-icon {
    @include apply-svg-color(pvar(--mainColor));
  }
}

@mixin tertiary-button {
  @include button-focus($grey-button-outline-color);

  color: pvar(--greyForegroundColor);
  background-color: transparent;

  &[disabled],
  .disabled {
    cursor: default;
  }

  my-global-icon {
    @include apply-svg-color(transparent);
  }
}

@mixin grey-button {
  @include button-focus($grey-button-outline-color);

  background-color: pvar(--greyBackgroundColor);
  color: pvar(--greyForegroundColor);

  &:hover,
  &:active,
  &:focus,
  &[disabled],
  &.disabled {
    color: pvar(--greyForegroundColor);
    background-color: pvar(--greySecondaryBackgroundColor);
  }

  &[disabled],
  &.disabled {
    cursor: default;
  }

  my-global-icon {
    @include apply-svg-color(pvar(--greyForegroundColor));
  }
}

@mixin danger-button {
  $color: color.adjust($color: #c54130, $lightness: 10%);
  $text: #fff6f5;

  @include button-focus(scale-color($color, $alpha: -95%));

  background-color: $color;
  color: $text;

  &:hover,
  &:active,
  &:focus,
  &[disabled],
  &.disabled {
    background-color: color.adjust($color: $color, $lightness: 10%);
  }

  &[disabled],
  &.disabled {
    cursor: default;
  }

  my-global-icon {
    @include apply-svg-color($text);
  }
}

@mixin peertube-button {
  @include rounded-line-height-1-5($button-font-size);

  padding: 4px 13px;

  border: 0;
  font-weight: $font-semibold;

  // Because of primeng that redefines border-radius of all input[type="..."]
  border-radius: 3px !important;

  text-align: center;
  cursor: pointer;

  font-size: $button-font-size;

  my-global-icon + * {
    @include margin-right(4px);
    @include margin-left(4px);
  }
}

@mixin peertube-button-big {
  height: auto;
  padding: 10px 25px;
  font-size: 18px;
  line-height: 1.2;
  border: 0;
  font-weight: $font-semibold;

  // Because of primeng that redefines border-radius of all input[type="..."]
  border-radius: 3px !important;
}

@mixin peertube-button-link {
  @include disable-default-a-behaviour;
  @include peertube-button;

  display: inline-block;
}

@mixin peertube-button-big-link {
  @include disable-default-a-behaviour;
  @include peertube-button-big;

  display: inline-block;
}

@mixin peertube-button-outline {
  @include disable-default-a-behaviour;
  @include peertube-button;

  display: inline-block;
  border: 1px solid;
}

@mixin button-with-icon($width: 20px, $margin-right: 3px, $top: -1px) {
  my-global-icon {
    @include margin-right($margin-right);

    position: relative;
    width: $width;
    top: $top;
  }
}

@mixin peertube-file {
  position: relative;
  overflow: hidden;
  display: inline-block;

  input[type=file] {
    position: absolute;
    top: 0;
    right: 0;
    width: 100%;
    height: 100%;
    font-size: 100px;
    text-align: end;
    filter: alpha(opacity=0);
    opacity: 0;
    outline: none;
    background: pvar(--mainBackgroundColor);
    cursor: inherit;
    display: block;
  }
}

@mixin peertube-button-file ($width) {
  @include peertube-file;
  @include peertube-button;

  width: $width;
}

@mixin icon ($size) {
  display: inline-block;
  background-repeat: no-repeat;
  background-size: contain;
  width: $size;
  height: $size;
  vertical-align: middle;
  cursor: pointer;
}

@mixin responsive-width ($width) {
  width: $width;

  @media screen and (max-width: $width) {
    width: 100%;
  }
}

@mixin peertube-select-container ($width) {
  padding: 0;
  margin: 0;
  width: $width;
  border-radius: 3px;
  color: pvar(--inputForegroundColor);
  background: pvar(--inputBackgroundColor);
  position: relative;
  height: min-content;

  &.disabled {
    background-color: #E5E5E5;

    select {
      cursor: default;
    }
  }

  select[disabled] {
    background-color: #f9f9f9;
  }

  @media screen and (max-width: $width) {
    width: 100%;
  }

  &::after {
    top: 50%;
    right: calc(0% + 15px);
    content: ' ';
    height: 0;
    width: 0;
    position: absolute;
    pointer-events: none;
    border: 5px solid rgba(0, 0, 0, 0);
    border-top-color: pvar(--mainForegroundColor);
    margin-top: -2px;
    z-index: 100;
  }

  select {
    @include rounded-line-height-1-5($form-input-font-size);

    font-size: $form-input-font-size;

    padding: 3px 35px 3px 12px;
    position: relative;
    border: 1px solid pvar(--inputBorderColor);
    background: transparent none;
    appearance: none;
    text-overflow: ellipsis;
    color: pvar(--mainForegroundColor);

    &:focus {
      outline: none;
    }

    &:-moz-focusring {
      color: transparent;
      text-shadow: 0 0 0 #000;
    }

    option {
      color: #000;

      &[value=undefined] {
        font-weight: $font-semibold;
      }
    }
  }

  &.peertube-select-button {
    @include grey-button;

    select {
      font-weight: $font-semibold;
      color: pvar(--greyForegroundColor);
      border: 0;

      // No border, add +1 to vertical padding
      padding: 4px 35px 4px 12px;
    }
  }
}

// Thanks: https://codepen.io/manabox/pen/raQmpL
@mixin peertube-radio-container {
  label {
    font-size: $form-input-font-size;
  }

  [type=radio]:checked,
  [type=radio]:not(:checked) {
    position: absolute;
    opacity: 0;
    cursor: pointer;
    height: 0;
    width: 0;
  }

  [type=radio]:checked + label,
  [type=radio]:not(:checked) + label {
    position: relative;
    padding-left: 28px;
    cursor: pointer;
    line-height: 20px;
    display: inline-block;
    font-weight: $font-regular;
  }

  [type=radio]:checked + label::before,
  [type=radio]:not(:checked) + label::before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    width: 18px;
    height: 18px;
    border: 1px solid pvar(--inputBorderColor);
    border-radius: 100%;
    background: #fff;
  }

  [type=radio]:checked + label::after,
  [type=radio]:not(:checked) + label::after {
    content: '';
    width: 10px;
    height: 10px;
    background: pvar(--mainColor);
    position: absolute;
    top: 4px;
    left: 4px;
    border-radius: 100%;
    transition: all 0.2s ease;
  }
  [type=radio]:not(:checked) + label::after {
    opacity: 0;
    transform: scale(0);
  }
  [type=radio]:checked + label::after {
    opacity: 1;
    transform: scale(1);
  }

  .form-group-description {
    display: block;
    margin-top: -7px;
    margin-bottom: 10px;
    margin-left: 29px;
  }
}

@mixin peertube-checkbox ($border-width) {
  opacity: 0;
  position: absolute;

  &:focus + span {
    box-shadow: #{$focus-box-shadow-form} pvar(--mainColorLightest);
  }

  + span {
    position: relative;
    width: 18px;
    min-width: 18px;
    height: 18px;
    border: $border-width solid pvar(--inputBorderColor);
    border-radius: 3px;
    vertical-align: middle;
    cursor: pointer;

    &::after {
      content: '';
      position: absolute;
      top: calc(2px - #{$border-width});
      left: 5px;
      width: 5px;
      height: 12px;
      opacity: 0;
      transform: rotate(45deg) scale(0);
      border-right: 2px solid pvar(--mainBackgroundColor);
      border-bottom: 2px solid pvar(--mainBackgroundColor);
    }
  }

  &:checked + span {
    border-color: transparent;
    background: pvar(--mainColor);
    animation: jelly 0.6s ease;

    &::after {
      opacity: 1;
      transform: rotate(45deg) scale(1);
    }
  }

  + span + span {
    @include margin-left(5px);

    font-weight: $font-regular;
    cursor: pointer;
    display: inline;
  }

  &[disabled] + span,
  &[disabled] + span + span {
    opacity: 0.5;
    cursor: default;
  }
}

@mixin actor-avatar-size ($size) {
  display: inline-block;
  width: $size;
  height: $size;
  min-width: $size;
  min-height: $size;
}

@mixin actor-counters ($separator-margin: 10px) {
  color: pvar(--greyForegroundColor);
  display: flex;
  align-items: center;

  > *:not(:last-child)::after {
    content: '•';
    margin: 0 $separator-margin;
    color: pvar(--mainColor);
  }
}

@mixin in-content-small-title {
  text-transform: uppercase;
  color: pvar(--mainColor);
  font-weight: $font-bold;
  font-size: 13px;
}

@mixin settings-big-title {
  text-transform: uppercase;
  color: pvar(--mainColor);
  font-weight: $font-bold;
  font-size: 1rem;
  margin-bottom: 10px;
}

@mixin row-blocks ($column-responsive: true, $min-height: 130px, $separator: true) {
  display: flex;
  min-height: $min-height;
  padding-bottom: 20px;
  margin-bottom: 20px;

  @if $separator {
    border-bottom: 1px solid pvar(--inputBorderColor);
  }

  @media screen and (max-width: $small-view) {
    @if $column-responsive {
      flex-direction: column;
      height: auto;
      align-items: center;
    } @else {
      min-height: initial;
      padding-bottom: 10px;
      margin-bottom: 10px;
    }
  }
}

@mixin dropdown-with-icon-item {
  padding: 6px 15px;

  my-global-icon {
    @include margin-right(10px);

    width: 22px;
    opacity: .7;
    position: relative;
    top: -2px;
  }
}

@mixin progressbar($small: false) {
  background-color: pvar(--greyBackgroundColor);
  display: flex;
  height: 1rem;
  overflow: hidden;
  font-size: 0.75rem;
  border-radius: 0.25rem;
  position: relative;

  span {
    position: absolute;
    color: pvar(--greyForegroundColor);

    @if $small {
      top: -1px;
    }

    &:nth-of-type(1) {
      left: .2rem;
    }
    &:nth-of-type(2) {
      right: .2rem;
    }
  }

  .progress-bar {
    color: pvar(--mainBackgroundColor);
    background-color: pvar(--mainColor);
    display: flex;
    flex-direction: column;
    justify-content: center;
    text-align: center;
    white-space: nowrap;
    transition: width 0.6s ease;

    &.red {
      background-color: color.adjust($color: #c54130, $lightness: 10%);
    }
  }
}

@mixin breadcrumb {
  display: flex;
  flex-wrap: wrap;
  padding: 0;
  margin-bottom: 1rem;
  list-style: none;
  font-weight: $font-semibold;

  .breadcrumb-item {
    display: flex;

    a {
      color: pvar(--mainColor);
    }

    + .breadcrumb-item {
      @include padding-left(0.5rem);

      &::before {
        @include padding-right(0.5rem);

        display: inline-block;
        color: #6c757d;
        content: '/';
      }
    }

    &.active {
      color: #6c757d;
    }
  }
}

@mixin dashboard {
  display: flex;
  flex-wrap: wrap;
  margin: 0 -5px;

  > div {
    box-sizing: border-box;
    flex: 0 0 math.percentage(math.div(1, 3));
    padding: 0 5px;
    margin-bottom: 10px;

    > a {
      @include disable-default-a-behaviour;

      text-decoration: none;
      color: inherit;
      display: block;
      font-size: 18px;

      &:active,
      &:focus,
      &:hover {
        opacity: .8;
      }
    }

    > a,
    > div {
      padding: 20px;
      background: pvar(--submenuBackgroundColor);
      border-radius: 4px;
      box-sizing: border-box;
      height: 100%;
    }
  }

  .dashboard-num,
  .dashboard-text {
    text-align: center;
    font-size: 130%;
    color: pvar(--mainForegroundColor);
    line-height: 30px;
    margin-bottom: 20px;
  }

  .dashboard-label {
    font-size: 90%;
    color: pvar(--inputPlaceholderColor);
    text-align: center;
  }
}

@mixin divider($color: pvar(--submenuBackgroundColor), $background: pvar(--mainBackgroundColor)) {
  width: 95%;
  border-top: .05rem solid $color;
  height: .05rem;
  text-align: center;
  display: block;
  position: relative;

  &[data-content] {
    margin: .8rem 0;

    &::after {
      background: $background;
      color: $color;
      content: attr(data-content);
      display: inline-block;
      font-size: .7rem;
      padding: 0 .4rem;
      transform: translateY(-.65rem);
    }
  }
}

@mixin chip {
  --avatar-size: 1.2rem;

  display: inline-flex;
  color: pvar(--mainForegroundColor);
  height: var(--avatar-size);
  max-width: 320px;
  overflow: hidden;
  text-decoration: none;
  text-overflow: ellipsis;
  vertical-align: middle;
  white-space: nowrap;

  my-actor-avatar {
    @include margin-right(.2rem);

    border-radius: 5rem;
    width: var(--avatar-size);
    height: var(--avatar-size);
  }

  &.two-lines {
    --avatar-size: 2rem;

    font-size: 14px;
    line-height: 1rem;

    my-actor-avatar {
      display: inline-block;
    }

    > div {
      display: flex;
      flex-direction: column;
      justify-content: center;
    }
  }
}

// applies ratio (default to 16:9) to a child element (using $selector) only using
// an immediate's parent size. This allows to set a ratio without explicit
// dimensions, as width/height cannot be computed from each other.
@mixin block-ratio ($selector: 'div', $inverted-ratio: math.div(9, 16)) {
  $padding-percent: math.percentage($inverted-ratio);

  position: relative;
  height: 0;
  width: 100%;
  padding-top: $padding-percent;

  #{$selector} {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;

    @content;
  }
}

@mixin sub-menu-h1 {
  ::ng-deep h1 {
    font-size: 1.3rem;
    border-bottom: 2px solid $grey-background-color;
    padding-bottom: 15px;
    margin-bottom: $sub-menu-margin-bottom;

    > span > my-global-icon,
    > my-global-icon {
      @include margin-right(10px);
      width: 24px;
      height: 24px;
      vertical-align: top;
    }

    .pt-badge {
      @include margin-left(7px);

      vertical-align: top;
    }
  }
}

@mixin play-icon ($width, $height) {
  width: 0;
  height: 0;

  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%) scale(0.5);

  border-top: #{math.div($height, 2)} solid transparent;
  border-bottom: #{math.div($height, 2)} solid transparent;

  border-left: $width solid rgba(255, 255, 255, 0.95);
}

@mixin on-small-main-col () {
  :host-context(.main-col:not(.expanded)) {
    @media screen and (max-width: #{$small-view + $menu-width}) {
      @content;
    }
  }

  :host-context(.main-col.expanded) {
    @media screen and (max-width: $small-view) {
      @content;
    }
  }
}

@mixin on-mobile-main-col () {
  :host-context(.main-col:not(.expanded)) {
    @media screen and (max-width: #{$mobile-view + $menu-width}) {
      @content;
    }
  }

  :host-context(.main-col.expanded) {
    @media screen and (max-width: $mobile-view) {
      @content;
    }
  }
}

@mixin margin ($arg1: null, $arg2: null, $arg3: null, $arg4: null) {
  @if $arg2 == null and $arg3 == null and $arg4 == null {
    @include margin-original($arg1, $arg1, $arg1, $arg1);
  } @else if $arg3 == null and $arg4 == null {
    @include margin-original($arg1, $arg2, $arg1, $arg2);
  } @else if $arg4 == null {
    @include margin-original($arg1, $arg2, $arg3, $arg2);
  } @else {
    @include margin-original($arg1, $arg2, $arg3, $arg4);
  }
}

@mixin margin-original ($block-start, $inline-end, $block-end, $inline-start) {
  @include margin-left($inline-start);
  @include margin-right($inline-end);
  @include margin-top($block-start);
  @include margin-bottom($block-end);
}

@mixin margin-left ($value) {
  @include rfs($value, margin-inline-start);
}

@mixin margin-right ($value) {
  @include rfs($value, margin-inline-end);
}

@mixin padding-original ($block-start, $inline-end, $block-end, $inline-start) {
  @include padding-left($inline-start);
  @include padding-right($inline-end);
  @include padding-top($block-start);
  @include padding-bottom($block-end);
}

@mixin padding ($arg1: null, $arg2: null, $arg3: null, $arg4: null) {
  @if $arg2 == null and $arg3 == null and $arg4 == null {
    @include padding-original($arg1, $arg1, $arg1, $arg1);
  } @else if $arg3 == null and $arg4 == null {
    @include padding-original($arg1, $arg2, $arg1, $arg2);
  } @else if $arg4 == null {
    @include padding-original($arg1, $arg2, $arg3, $arg2);
  } @else {
    @include padding-original($arg1, $arg2, $arg3, $arg4);
  }
}

@mixin padding-left ($value) {
  @include rfs($value, padding-inline-start);
}

@mixin padding-right ($value) {
  @include rfs($value, padding-inline-end);
}
